package com.controller.purchase;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.service.purchase.BillGroupService;
import com.model.purchase.BillGroupModel;
import com.model.purchase.BillVoModel;

/**
 * 价格维护
 */
@Controller("billGroupController")
@RequestMapping(value = "/api")
public class BillGroupController {
	static final Logger logger = LoggerFactory.getLogger(BillGroupController.class);

	@Autowired
	BillGroupService billGroupService;

	/**
	 * 查询审核列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "purchase/bill/group/selectList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectList(@RequestParam Map<String, Object> map) {
		return billGroupService.selectList(map);
	}

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "purchase/bill/group/selectCheckList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectCheckList(@RequestParam Map<String, Object> map) {
		return billGroupService.selectCheckList(map);
	}

	/**
	 * 查询详情
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "purchase/bill/group/selectInfor", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectInfor(@RequestParam String id) {
		return billGroupService.selectInfor(id);
	}

	/**
	 * 保存询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "purchase/bill/group/save", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult save(@RequestBody BillVoModel billVoModel) {
		return billGroupService.save(billVoModel);
	}

	/**
	 * 发票审核保存询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "purchase/bill/group/check", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult check(@RequestBody BillVoModel billVoModel) {
		return billGroupService.check(billVoModel);
	}

	/**
	 * 删除询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "purchase/bill/group/delete", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delete(@RequestParam String id) {
		return billGroupService.delete(id);
	}

	/**
	 * 退回询价
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "purchase/bill/group/back", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult back(@RequestParam String id) {
		return billGroupService.back(id);
	}

	@RequestMapping(value = "purchase/bill/group/recall", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult recall(@RequestParam("groId") String groId, @RequestParam("status") String status) {
		return billGroupService.recall(groId, status);
	}

	@RequestMapping(value = "purchase/bill/group/recallAdmin", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult recallAdmin(@RequestParam("groId") String groId, @RequestParam("status") String status) {
		return billGroupService.recallAdmin(groId, status);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "purchase/bill/group/export")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		billGroupService.export(map, response);

	}
}
